﻿@inherits BootstrapChildContentComponentBase
@namespace Bootstrap5

<div class="accordion @(GetCssClassString())" @attributes="AdditionalAttributes" id="@Alias">
    <CascadingValue Value="this">
        @ChildContent
    </CascadingValue>
</div>

@code{
    [Parameter][CssClass("accordion-flush")] public bool Flush { get; set; }
    [Parameter] public int ActiveIndex { get; set; } = 0;
    [Parameter] public string Alias { get; set; } = $"accordion-{Guid.NewGuid()}";
    [Parameter] public bool AlwaysOpen { get; set; }

    internal List<AccordionItem> Components { get; set; }=new List<AccordionItem>();

    internal void AddComponent(AccordionItem item)
    {
        if (item is null)
        {
            throw new ArgumentNullException(nameof(item));
        }
        Components.Add(item);
        if (ActiveIndex >= 0 && ActiveIndex < Components.Count)
        {
            Components[ActiveIndex].Active = true;
        }
        StateHasChanged();
    }
}